/**
 * Created by dale on 2017/10/9.
 */
var carList;
$(document).ready(function () {
    carList = $('#carList');
    $('.xh-datagrid-toolbar').parseToolbar();

    carList.initDataGrid(baseUrl + 'lm/car/getCar').setSelectThenEnable(['bt_list_del'],['bt_list_startUse','bt_list_endUse']);

    $('#bt_list_add').click(function () {
        window.top.xhTop.showModal({
            title:'新增车辆',
            content: '<iframe src="'+baseUrl+'lm/car/carForm"  style="width: 100%;;border-style: none;padding: 0px;height:150px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
        });
    });

    $('#bt_list_del').click(function () {
        var ids=carList.getDataGridSelectedFields('iCarId');
        myWindow.initPopUp({title: '确定要删除么?', text: '这些车辆将被删除!'},function () {
            $.ajax({
                url:baseUrl+'lm/car/delCar',
                data:{ids:ids},
                type:'POST',
                success:function (data) {
                    window.top.swal(
                        data,
                        '',
                        'success'
                    );
                    carList.dataGridReload();
                },
                error:function (data) {
                    window.top.swal(
                        data['responseText'],
                        '',
                        'error'
                    );
                }
            });
        });
    });

    $('#bt_list_startUse').click(function () {
        if(xhExtra.buttonDisabled($(this))){ return false }
        var iUCId = carList.getDataGridSelectedField('iUCId');
        if(iUCId==null){
            window.top.swal(
                '',
                '未找到关联用车记录',
                'error'
            );
            return false;
        }
        window.top.xhTop.showModal({
            title:'取车确认',
            content: '<iframe src="'+baseUrl+'lm/car/confirm/'+iUCId+'/1"  style="width: 100%;;border-style: none;padding: 0px;height:250px"  scrolling="yes"   ></iframe>',
            button:'提交'
        });
    });

    $('#bt_list_endUse').click(function () {
        if(xhExtra.buttonDisabled($(this))){ return false }
        var iUCId = carList.getDataGridSelectedField('iUCId');
        if(iUCId==null){
            window.top.swal(
                '',
                '未找到关联用车记录',
                'error'
            );
            return false;
        }
        window.top.xhTop.showModal({
            title:'还车确认',
            content: '<iframe src="'+baseUrl+'lm/car/confirm/'+iUCId+'/2"  style="width: 100%;;border-style: none;padding: 0px;height:250px"  scrolling="yes"   ></iframe>',
            button:'提交'
        });
    });

    $('#bt_list_Edit').click(function () {
        var iCarId = carList.getDataGridSelectedField('iCarId');
        window.top.xhTop.showModal({
            title:'编辑车辆',
            content:'<iframe src="'+baseUrl+'lm/car/carForm?iCarId='+iCarId+'" style="height: 150px;border-style: none;padding: 0px;width: 100%;"></iframe>',
            button:'提交',
            backClose: false
        });
    });
    
    DataGrid.onDblClickRow=function (rowIndex, rowData) {
        var iCarId = rowData['iCarId'];
        var sCarName = rowData['sCarName'];
        window.top.xhTop.showModal({
            title:sCarName+'-用车列表',
            content:'<iframe src="'+baseUrl+'lm/car/useCarList/read?iCarId='+iCarId+'" style="height: 550px;border-style: none;padding: 0px;width: 100%;"></iframe>',
            size:'lg',
            backClose: false
        });
        return false;
    }
});